const express = require('express')
const app = express()
const axios = require('axios')
const ejs = require('ejs')
const baseUrl = 'http://192.168.1.28:8812'

// 设置模板引擎
app.engine('html', ejs.__express)
app.set('view engine', 'html')

// 静态资源
app.use(express.static(__dirname + '/public'))

app.get('/getSchoolStory', async (req, res) => {
    const page = req.query.pageNum
    const { data } = await axios.get(`${baseUrl}/api/Common/GetSchoolStoryList?pageSize=3&pageNum=${page}`)

    res.send(data)
})

app.get('/getSchoolPic', async (req, res) => {
    const id = req.query.id
    const { data } = await axios.get(`${baseUrl}/api/Common/GetSchoolDisplayDetailList?schoolDisplayId=${id}`)

    res.send(data.data)
})

app.get('/', async (req, res) => {
    const { data: BrandManagement } = await axios.get(`${baseUrl}/api/Common/GetBrandPublicityList`)
    const { data: VideoInterview } = await axios.get(`${baseUrl}/api/Common/GetVideoInterviewList`)
    const { data: SchoolStory } = await axios.get(`${baseUrl}/api/Common/GetSchoolStoryList?pageNum=1&pageSize=3`)
    const { data: SchoolDisplay } = await axios.get(`${baseUrl}/api/Common/GetSchoolDisplayList`)

    res.render('index', {
        BrandManagement: BrandManagement.data,
        VideoInterview: VideoInterview.data.slice(0, 3),
        SchoolStory: SchoolStory.data,
        SchoolDisplay: SchoolDisplay.data.slice(0, 9)
    })
})

app.get('/article', async (req, res) => {
    const id = req.query.id
    const { data: Article } = await axios.get(`${baseUrl}/api/Common/SchoolStoryDetail?id=${id}`)

    res.render('article', { Article: Article.data })
})

app.get('/*', (req, res) => {
    res.redirect(302, '/')
})

app.listen(8002, () => console.log('服务启动成功...'))